using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using XNAPlatformer.Source.Characters;

namespace XNAPlatformer.Source.Levels
{
    public class Level
    {
        private ContentManager content;
        public List<Texture2D> textures;
        public List<Texture2D> alphas;
        public Texture2D levelTexture;
        public Texture2D alphaMap;
        public int focus;
        public bool levelEnd;
        private float spawnTime = 5.0f;
        public Color[] alphaMapColors;

        public Level(ContentManager content)
        {
            textures = new List<Texture2D>();
            alphas = new List<Texture2D>();
            textures.Add(content.Load<Texture2D>("Content\\Textures\\Levels\\tempLevel1"));
            textures.Add(content.Load<Texture2D>("Content\\Textures\\Levels\\tempLevel2"));
            alphas.Add(content.Load<Texture2D>("Content\\Textures\\Levels\\tempLevel1Alpha"));
            alphas.Add(content.Load<Texture2D>("Content\\Textures\\Levels\\tempLevel2Alpha"));
            levelTexture = textures[0];
            alphaMap = alphas[0];
            this.content = content;
            focus = 0;
            alphaMapColors = new Color[textures[0].Width * textures[0].Height];
            alphaMap.GetData(alphaMapColors);
        }

        public void Update(GameTime gameTime, GamePadState gamePad, bool moveMap)
        {
            

            if (moveMap == true)
            {

                focus -= 3;
            }

            if (focus < -637 && levelEnd == false)
            {
                int index = textures.IndexOf(levelTexture);

                if (index + 1 < textures.Count)
                {
                    levelTexture = textures[index + 1];
                    alphaMap = alphas[index + 1];
                    alphaMap.GetData(alphaMapColors);
                    focus = 0;
                }

                if (index + 1 == textures.Count - 1)
                {
                    levelEnd = true;
                }
            }

            CheckEnemySpawning(gameTime);
        }

        public void Draw(GameTime gameTime, SpriteBatch spritebatch)
        {
            spritebatch.Draw(levelTexture, new Rectangle(focus, 0, 1280, 480), Color.White);  
        }

        public Texture2D GetCurrentTexture()
        {
            return alphaMap;
        }

        public int GetCurrentFocus()
        {
            return focus;
            
        }

        public int GetCurrentLevelPosition()
        {
           return (int)(alphas.IndexOf(GetCurrentTexture()) * 640 + (GetCurrentFocus() * -1));
        }

        public void CheckEnemySpawning(GameTime gameTime)
        {

            spawnTime -= (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (GetCurrentLevelPosition() > 625 && spawnTime < 0)
            {
                XNAPlatformer.enemies.Add(new Enemy(content, new Vector2(0,0), new Vector2(1,0)));
                spawnTime = 5.0f;
            }
        }
        
    }
}
